package com.yswl.yeeduo.ui.activity;

import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.analytics.MobclickAgent;
import com.yswl.yeeduo.R;
import com.yswl.yeeduo.http.KAsyncTask.TaskDelegate;
import com.yswl.yeeduo.tool.KStringTool;
import com.yswl.yeeduo.ui.adapter.RankingDeitalAdapter;
import com.yswl.yeeduo.ui.base.BaseActivity;
import com.yswl.yeeduo.ui.bean.RankingDetailBean;
import com.yswl.yeeduo.ui.bean.RankingDetailBean.RankingDetailListItemBean;
import com.yswl.yeeduo.ui.task.RankingDetailTask;
import com.yswl.yeeduo.ui.tool.ServiceAuthManager;
import com.yswl.yeeduo.ui.utils.ImageUtil;

/**
 * 查看排行榜 排行榜详情页
 * 
 * @author Administrator
 * 
 */
public class RankingListDetailActivity extends BaseActivity implements TaskDelegate {

	public static Intent createIntent(Context context, String active_id) {
		Intent returnIntent = new Intent(context, RankingListDetailActivity.class);
		returnIntent.putExtra("activeId", active_id);
		return returnIntent;
	}

	private String activId;

	private ListView mRankingDetailList;
	private RankingDeitalAdapter mAdapter;
	protected ImageLoader imageLoader;
	protected DisplayImageOptions mOptions;
	private RankingDetailBean mData;

	private ImageView imgView;
	private TextView title;
	private TextView readReward;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user_rankingdetail);
		super.setHeader("排行榜详情");
		initUI();
		getData();
	}

	private void initUI() {
		activId =  getIntent().getStringExtra("activeId");

		mOptions = ImageUtil.getHeadDefaultOptions();
		imageLoader = ImageLoader.getInstance();

		imgView = (ImageView) findViewById(R.id.img_banner);
		title = (TextView) findViewById(R.id.tv_rankingdetail_title);
		readReward = (TextView) findViewById(R.id.tv_rankingdetail_readreward);
		mRankingDetailList = (ListView) findViewById(R.id.lv_rankingdetail);
		mAdapter = new RankingDeitalAdapter(this);
		mRankingDetailList.setAdapter(mAdapter);

		Button readBtn = (Button) findViewById(R.id.btn_rankingdetail_readaticl);
		readBtn.setOnClickListener(new OnClickListener() {// 阅读全文
			@Override
			public void onClick(View v) {
				if (mData != null) {
					startActivity(WebViewActivity2.createIntent(RankingListDetailActivity.this, mData.active.url, mData.active.title, false, WebViewActivity2.class));
				}
			}
		});
	}

	private void getData() {
		mAdapter.dataList.clear();
		if (mData == null) {
			// 异步任务 请求数据
			Map<String, String> params = new TreeMap<String, String>();
			String activityId = activId;
			params.put("activeId", activityId);
			String userId = ServiceAuthManager.getUserId(this);
			if (KStringTool.isEmpty(userId)) {
				userId = "-1";
			}
			params.put("userId", userId);
			params.put("api_key", ServiceAuthManager.getToken(this));
			new RankingDetailTask(this, -9999, this).execute(params);
		} else {
			mAdapter.setData((ArrayList<RankingDetailListItemBean>) mData.rank);
		}

	}

	// Um
	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart(this.getClass().getSimpleName());
		MobclickAgent.onResume(this); // 统计时长
	}

	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd(this.getClass().getSimpleName());
		MobclickAgent.onPause(this);
	}

	@Override
	public void faild_TaskDelegate(int taskId, Object object) {
	}

	@Override
	public void success_TaskDelegate(int taskId, Object object) {
		mData = (RankingDetailBean) object;

		imageLoader.displayImage(mData.active.coverImg, imgView, mOptions);
		title.setText(mData.active.title);
		readReward.setText(mData.active.readReward);
		mAdapter.setData((ArrayList<RankingDetailListItemBean>) mData.rank);
	}

}
